import {API} from '../config/http.config'
import {removeVaAPI} from '../utils/mock'
import  { Orders, Categories, Products, Users, Role } from '../config/mock.config'
import {getOrInit, setToLS} from '../utils/mock'


export function setupDashBoardMock(mock){
    // GET
    mock.onGet(removeVaAPI(API.getOrdersById)).reply(config => {
        const data = getOrInit('Orders', Orders);
        return [200, data];
    });

    mock.onGet(removeVaAPI(API.getCategoriesById)).reply(config => {
        const data = getOrInit('Categories', Categories);
        console.log(':::', data);
        return [200, data];
    });

    mock.onGet(removeVaAPI(API.getProductsById)).reply(config => {
        const data = getOrInit('Products', Products);
        return [200, data];
    });

    mock.onGet(removeVaAPI(API.getUsersById)).reply(config => {
        const data = getOrInit('Users', Users);
        return [200, data];
    });

    mock.onGet(removeVaAPI(API.getRoles)).reply(config => {
        return [200, Role];
    });

    // UPDATE
    mock.onPost(removeVaAPI(API.updateCategories)).reply(config => {
        let data = config.data;
        if (typeof data === 'string') data = JSON.parse(data);
        let categories = getOrInit('Categories', Categories);

        // 更新已有项
        const updatedCategories = categories.map(c => {
            const updated = data.find(item => item.id === c.id);
            return updated ? { ...c, ...updated } : c;
        });
        // 添加新项
        data.forEach(item => {
            if (!updatedCategories.find(c => c.id === item.id)) {
                updatedCategories.push(item);
            }
        });
        setToLS('Categories', updatedCategories);
        return [200, updatedCategories];
    });

    mock.onPost(removeVaAPI(API.updateProducts)).reply(config => {
        let data = config.data;
        if (typeof data === 'string') data = JSON.parse(data);
        let products = getOrInit('Products', Products);

        const updatedProducts = products.map(product => {
            const updated = data.find(item => item.id === product.id);
            return updated ? { ...product, ...updated } : product;
        });
        data.forEach(item => {
            if (!updatedProducts.find(p => p.id === item.id)) {
                updatedProducts.push(item);
            }
        });
        setToLS('Products', updatedProducts);
        return [200, updatedProducts];
    });

    mock.onPost(removeVaAPI(API.updateUsers)).reply(config => {
        let data = config.data;
        if (typeof data === 'string') data = JSON.parse(data);
        let users = getOrInit('Users', Users);

        const updatedUsers = users.map(user => {
            const updated = data.find(item => item.id === user.id);
            return updated ? { ...user, ...updated } : user;
        });
        data.forEach(item => {
            if (!updatedUsers.find(u => u.id === item.id)) {
                updatedUsers.push(item);
            }
        });
        setToLS('Users', updatedUsers);
        return [200, updatedUsers];
    });

    mock.onPost(removeVaAPI(API.updateOrders)).reply(config => {
        let data = config.data;
        if (typeof data === 'string') data = JSON.parse(data);
        let orders = getOrInit('Orders', Orders);

        const updatedOrders = orders.map(order => {
            const updated = data.find(item => item.id === order.id);
            return updated ? { ...order, ...updated } : order;
        });
        data.forEach(item => {
            if (!updatedOrders.find(o => o.id === item.id)) {
                updatedOrders.push(item);
            }
        });
        setToLS('Orders', updatedOrders);
        return [200, updatedOrders];
    });

    // DELETE
    mock.onDelete(removeVaAPI(API.deleteOrders)).reply(config => {
        let data = config.data;
        const orderId = JSON.parse(data).params.id;
        let orders = getOrInit('Orders', Orders);
        orders = orders.filter(item => item.id !== orderId);
        setToLS('Orders', orders);
        return [200, orders];
    });

    mock.onDelete(removeVaAPI(API.deleteCategories)).reply(config => {
        let data = config.data;
        const categoryId = JSON.parse(data).params.id;
        let categories = getOrInit('Categories', Categories);
        categories = categories.filter(item => item.id !== categoryId);
        setToLS('Categories', categories);
        return [200, categories];
    });

    mock.onDelete(removeVaAPI(API.deleteProducts)).reply(config => {
        let data = config.data;
        const productId = JSON.parse(data).params.id;
        let products = getOrInit('Products', Products);
        products = products.filter(item => item.id !== productId);
        setToLS('Products', products);
        return [200, products];
    });

    mock.onDelete(removeVaAPI(API.deleteUsers)).reply(config => {
        let data = config.data;
        const userId = JSON.parse(data).params.id;
        let users = getOrInit('Users', Users);
        users = users.filter(item => item.id !== userId);
        setToLS('Users', users);
        return [200, users];
    });
}